<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ContactValidator
 *
 * @author Moon
 */
class ContactValidator extends CValidator{
    //put your code here

    public $name = '';
    
    public function validateAttribute($model,$attribute)
    {
        $value = $model->$attribute;
        $_name = $this->name;
        $name = $model->$_name;
        $pattern = "<< ([A-Za-z0-9 \n\r]*) >>";
        if(count(preg_match($pattern,$name))!=0)
            return;
        if($this->name == null || $this->name == '')
        {
            $message=$this->message!==null?$this->message:Yii::t('yii','Contact name must be indicated');
            $this->addError($model,$this->name,$message);
            
        }
        elseif($value == '' || $value == 0)
        {
            $message=$this->message!==null?$this->message:Yii::t('yii','Contact fetal error has occur, please contact the admin');
            $this->addError($model,$this->name,$message);
        }


        $Critera = new CDbCriteria;
        $Critera->condition=$attribute.'=:id';
        $Critera->select = array('cname');
        $Critera->params=array(':id'=>$value);
        $Checkmodel = customer::model()->find($Critera);
        if($Checkmodel==null)
        {
           $message=$this->message!==null?$this->message:Yii::t('yii','Contact is not found');
           $this->addError($model,$this->name,$message);
        }
        elseif(strtoupper($Checkmodel->cname) != strtoupper($name))
        {
           $message=$this->message!==null?$this->message:Yii::t('yii','Contact error');
           $this->addError($model,$this->name,$message);
        }

    }
}
?>
